//
// Created by martin on 8/3/21.
//

#ifndef FTPSERVER2_0_SESSION_H
#define FTPSERVER2_0_SESSION_H

#include <event2/bufferevent.h>
#include "user.h"
#include "dir.h"

typedef enum {
    DataTransferStatus_Idle = 0,
    DataTransferStatus_Start,
    DataTransferStatus_Finished,
    DataTransferStatus_Error
}DataTransferStatus;

/**
 * Session info for one conversion
 */
typedef struct {
    // request name of FTP command
    char cmd_name[5];
    // request text of FTP command (exclude request name, <SP> in the head or tail, \r\n in the tail )
    char cmd_text[1024];

    // PASV mode, data channel
    char serv_ip[INET_ADDRSTRLEN]; /* server ipv4 address */
    uint16_t serv_data_port;   /* server data port */

    // PORT mode, data channel
    char client_ip[INET_ADDRSTRLEN];  /* client ipv4 address */
    uint16_t client_data_port; /* client data port */

    // file transfer type
    int binary_flag; /* 1: binary mode; 0: text mode */

    int send_data_fd;
    int64_t f_offset; // file offset
    int64_t s_offset; // segment offset
    struct evbuffer *evbuf;
    struct evbuffer_file_segment *segment;
    off_t offset;
    DataTransferStatus send_data_status;
    int recv_data_fd;
    DataTransferStatus recv_data_status;

    User user; /* login user info */
    Dir *dir; /* ftp directory */

    char rename_old_path[256];
    char rename_new_path[256];

    // bufferevent for control channel
    struct bufferevent *bev;
    // bufferevent for data channel
    struct bufferevent *data_bev;
}Session;

Session *session_new();
void session_free(Session *s);

#endif //FTPSERVER2_0_SESSION_H
